﻿using System;
using System.Collections.Generic;

public class LinkedListNode<T>
{
    public T Value { get; set; }
    public LinkedListNode<T> Next { get; set; }

    public LinkedListNode(T value)
    {
        Value = value;
    }
}

public class LinkedList<T>
{
    private LinkedListNode<T> head;

    public void Add(T value)
    {
        if (head == null)
        {
            head = new LinkedListNode<T>(value);
        }
        else
        {
            LinkedListNode<T> current = head;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = new LinkedListNode<T>(value);
        }
    }

    public void ForEach(Action<T> action)
    {
        LinkedListNode<T> current = head;
        while (current != null)
        {
            action(current.Value);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main()
    {
        LinkedList<int> list = new LinkedList<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
        list.Add(4);
        list.Add(5);

        // 打印链表元素
        list.ForEach(x => Console.WriteLine(x));

        // 求最大值
        int max = int.MinValue;
        list.ForEach(x => { if (x > max) max = x; });
        Console.WriteLine($"Max: {max}");

        // 求最小值
        int min = int.MaxValue;
        list.ForEach(x => { if (x < min) min = x; });
        Console.WriteLine($"Min: {min}");

        // 求和
        int sum = 0;
        list.ForEach(x => sum += x);
        Console.WriteLine($"Sum: {sum}");
    }
}